﻿using System;
using System.Diagnostics.Contracts;
using System.Linq;

namespace Scrabble.Model
{
    public class BasicTimeKeeper : ITimeKeeper
    {
        private readonly IGame _game;
        private readonly DateTime _timeStarted;

        public BasicTimeKeeper(IGame game) : this(game, DateTime.Now)
        {
        }

        public BasicTimeKeeper(IGame game, DateTime timeStarted)
        {
            Contract.Requires<ArgumentNullException>(game != null);

            _game  = game;
            _timeStarted = timeStarted;
        }

        public TimeSpan TimeInPlay
        {
            get { return DateTime.Now - TimeOfLastPlay; }
        }

        public DateTime TimeOfLastPlay
        {
            get { return _game.EventLog.Aggregate(_timeStarted, (result, record) => result + record.DurationOfPlay); }
        }
    }
}